package com.yunhe.rentcars.pro.controller.system;


import com.yunhe.rentcars.commons.dto.JSONResponse;
import com.yunhe.rentcars.commons.dto.Pageable;
import com.yunhe.rentcars.pro.entity.system.Dept;
import com.yunhe.rentcars.pro.service.system.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;
    @RequestMapping
    public String deptList(){
        return "system/dept/deptlist";
    }


    @RequestMapping("/add")
    public String add(){
        return "system/dept/deptadd";
    }

    @RequestMapping("/listData")
    @ResponseBody
    public Pageable<Dept> listData(Integer offset, Integer limit){
        return deptService.findPage(offset,limit);
    }

    //跳转到编辑页面
    @RequestMapping("/edit")
    public String edit(Long id, HttpServletRequest request){
        Dept dept = deptService.findOne(id);
        request.setAttribute("dept",dept);
        return "system/dept/deptedit";
    }

    //保存方法
    @RequestMapping("/save")
    @ResponseBody
    public JSONResponse save(Dept dept){
        deptService.save(dept);
        return JSONResponse.success("保存成功！");
    }
//删除方法
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResponse delete(String ids){
        if(null!=ids){
            String[] split = ids.split(",");
            for(String id: split){
                deptService.delete(Long.valueOf(id));
            }
        }
        return JSONResponse.success("删除成功！");
    }

    //编辑方法
    @RequestMapping("/update")
    @ResponseBody
    public JSONResponse update(Dept dept){
        deptService.update(dept);
        return JSONResponse.success("修改成功");
    }

}
